/*  _FormatWritingImageView.m (Formerly ImageView.m)
 *  Purpose: When a new TIFF or EPS image is opened, a window is created and
 * 	an instance of this class -- ImageView -- is installed as the contentView
 * 	for the window.  This ties the NXImage instance together with the view.
 *
 *  You may freely copy, distribute, and reuse the code in this example.
 *  NeXT disclaims any warranty of any kind, expressed or  implied, as to its fitness
 *  for any particular use.
 *
 */
#import "_FormatWritingImageView.h"

@implementation _FormatWritingImageView : View

+ new
{
	static Window *win = nil;
	static _FormatWritingImageView *iv = nil;
	
	if (!iv) {
    	NXRect frm = {{0.0, 0.0}, {0.0, 0.0}};

		win = [[Window alloc] initContent:&frm style:NX_PLAINSTYLE backing:NX_BUFFERED buttonMask:0 defer:NO];
		[win setDepthLimit:NX_TwelveBitRGBDepth];
		iv = [[_FormatWritingImageView alloc] initFrame:&frm];
		[[win setContentView:iv] free]; // nuke the old one
		[iv allocateGState]; // neccessary? or voodoo?
	}
	return iv;
}

- useImage: newImage
{
    NXRect imageRect = {{0.0, 0.0}, {0.0, 0.0}};
	
    [newImage getSize:&(imageRect.size)];
    [super setFrame:&imageRect];[[self window] placeWindow:&imageRect];
    anImage = newImage;
    return self;
}

- image
{
    return anImage;
}

- drawSelf:(NXRect *)rects :(int)rectCount
{
    NXPoint pt = {0.0, 0.0};
	static BOOL _firstTime = 1;
    
	if (_firstTime) {
 		NXSetColor(NX_COLORBROWN);
 		NXRectFill(rects);
		_firstTime = NO;
	}
    NXSetColor(NX_COLORWHITE);
    NXRectFill(rects);
	// is there anything we can do to force this to display in color?
    [anImage composite: NX_SOVER  toPoint: &pt];
    return self;
}

- free
{
    return self;
}

- dumpTIFF:(const char *)tempPath {
	NXStream *memstream;
	id	targetRep;

	[self lockFocus];
	[self drawSelf:&frame :1];
	targetRep = [[NXBitmapImageRep alloc] initData:NULL fromRect:&frame];
	[self unlockFocus];
	memstream = NXOpenMemory(NULL, 0, NX_READWRITE);
	if (targetRep) {
		[targetRep setAlpha:0];
		[targetRep writeTIFF:memstream];
	}
	NXSaveToFile(memstream,tempPath);
	NXCloseMemory(memstream, NX_FREEBUFFER);
	[targetRep free];
	return self;
}

@end